package org.example.objevent.process;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ProcessManager {

    @Autowired
    private ApplicationContext applicationContext;

    private List<EndpointProcessor> processList = new ArrayList<>();
    private Map<String, List<EndpointProcessor>> endpointProcessorMap = new ConcurrentHashMap<>();

    public boolean acceptMessage(Object message){

        for (EndpointProcessor e : processList) {


        }

        return false;

    }

    public void processMessage(Object message){

    }

    @PostConstruct
    private void setup(){

        applicationContext.getBeansOfType(EndpointProcessor.class).forEach((key, value)->{

            processList.add(value);

            value.supportsType().forEach(type->{

                List<EndpointProcessor> endpointProcessorList = endpointProcessorMap.get(type);

                if(null == endpointProcessorList){
                    endpointProcessorList = new ArrayList<>();
                    endpointProcessorMap.put(type, endpointProcessorList);

                }

                endpointProcessorList.add(value);

            });

        });

    }

}
